import {Comment,Post} from '../db/schema2.js';

async function routes (fastify, options) {
  fastify.get('/', async (request, reply) => {
    const postsWithComments = await Post.findAll({
 //   where: { id: 1 },
  include: [
    { 
      model: Comment, 
      as: 'comments', 
      attributes: ['id', 'content', 'user_name', 'created_at'] // 只返回需要的评论字段
    }
  ],
  attributes: ['id', 'title', 'content', 'author_name'], // 只返回需要的帖子字段
  raw: false, // 关闭raw模式，让Sequelize返回实例对象（包含嵌套关联）
  nest: true  // 启用嵌套结构
});

// 如果需要JSON格式，可转换为纯对象
const result = postsWithComments.map(post => post.toJSON());
console.log(result);
    return result;
  })
}
//ESM
export default routes;